package com.ruoyi.service;

import com.google.gson.Gson;
import com.ruoyi.domain.DeviceAlarm;
import com.ruoyi.domain.DeviceReport;
import com.ruoyi.utils.RedisCache;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

@Component
public class SaveInfoToRedisService {

    private static final Logger log = LoggerFactory.getLogger(SaveInfoToRedisService.class);
    // 设备 id 列表在 Redis 中的 key
    private static final String DEVICE_IDS_KEY = "HEALTH_WEARABLE_DEVICEIDS";
    // 设备上报数据在 Redis 中的 key 前缀
    private static final String REPORT_KEY = "HEALTH_WEARABLE_REPORT_";
    // 设备告警信息在 Redis 中的 key 前缀（代码里有定义但方法未用到示例，保留常量）
    private static final String ALARM_KEY = "HEALTH_WEARABLE_ALARM_";
    // Gson 实例，用于对象和 JSON 互转
    private static final Gson gson = new Gson();

    // 注入 Redis 操作工具类（需项目里有对应的 RedisCache 实现）
    @Autowired
    private RedisCache redisCache;

    /**
     * 将设备 id 存入 Redis
     * @param deviceId 设备标识
     */
    public void saveDeviceIdsToRedis(String deviceId) {
        try {
            if (StringUtils.isBlank(deviceId)) {
                log.info("saveDeviceIdsToRedis deviceId is null:{},skip...", deviceId);
                return;
            }
            log.info("start saveDeviceIdsToRedis deviceId:{}", deviceId);
            if (redisCache.hasKey(DEVICE_IDS_KEY)) {
                log.info("exist key:{}", DEVICE_IDS_KEY);
                redisCache.addCacheSet(DEVICE_IDS_KEY, deviceId);
            } else {
                Set<String> set = new HashSet<>();
                set.add(deviceId);
                redisCache.setCacheSet(DEVICE_IDS_KEY, set);
            }
        } catch (Exception e) {
            log.error("saveDeviceIdsToRedis error,deviceId:{} msg:{}", deviceId, e.getMessage());
        }
    }

    /**
     * 将设备上报数据存入 Redis
     * @param deviceId 设备标识
     * @param report 设备上报数据实体（DeviceReport 类需提前定义好）
     */
    public void saveDeviceReportToRedis(String deviceId, DeviceReport report) {
        try {
            if (report == null) {
                log.info("deviceId:{} saveDeviceReportToRedis report is null:{},skip...", deviceId, report);
                return;
            }
            log.info("deviceId:{} start saveDeviceReportToRedis DeviceReport:{}",
                    deviceId, gson.toJson(report));
            String reportRedisKey = REPORT_KEY + deviceId;
            if (redisCache.hasKey(reportRedisKey)) {
                redisCache.addCacheList(reportRedisKey, report);
            } else {
                redisCache.setCacheList(reportRedisKey, Collections.singletonList(report));
            }
        } catch (Exception e) {
            log.error("saveDeviceReportToRedis error,deviceId:{} msg:{}", deviceId, e.getMessage());
        }
    }

    public void saveAlarmToRedis(String deviceId, DeviceAlarm alarm) {
        try {
            if(alarm == null){
                log.info("deviceId:{} start saveAlarmToRedis alarm is null:{},skip...",deviceId,alarm);
                return;
            }
            log.info("deviceId:{} start saveAlarmToRedis alarm:{}",deviceId,gson.toJson(alarm));
            if (redisCache.hasKey(ALARM_KEY+deviceId)){
                redisCache.addCacheList(ALARM_KEY+deviceId, alarm);
            } else {
                redisCache.setCacheList(ALARM_KEY+deviceId, Collections.singletonList(alarm));
            }
        } catch (Exception e){
            log.error("saveAlarmToRedis error,deviceId:{} msg:{}",deviceId, e.getMessage());
        }
    }
}